/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Util;

import Model.PostInterface;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import org.primefaces.context.RequestContext;

/**
 *
 * @author pedro
 */
public class FiltroUtil {
    
    public  static  String urlServer=ConfUtil.url; 

    public static ArrayList<PostInterface> filtro(List<String> selectedFilter) {
       
            ArrayList<PostInterface> coordenates = new ArrayList();
        try {
            RequestContext context = RequestContext.getCurrentInstance();
            FacesMessage msg = null;
            boolean loggedIn = false;

            URL url = new URL(urlServer+"filtroPost");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "text/plain");
            Gson gson = new Gson();
            String json = gson.toJson(selectedFilter);

            System.out.println(json);
            OutputStream os = conn.getOutputStream();
            
            os.write(json.getBytes());
            os.flush();

            if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output;

            Type collectionType = new TypeToken<List<PostInterface>>() {
            }.getType();
            while ((output = br.readLine()) != null) {

                coordenates = gson.fromJson(output, collectionType);

            }

            conn.disconnect();




        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        return coordenates;

    }
    
}
